package com.sg.zhui.visitor.utils.clip;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.utils.flyn.Eyes_Visitor;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;


/**
 * 头像裁剪Activity
 */
public class ClipImageActivity_Visitor extends AppCompatActivity {
    private static final String TAG = "ClipImageActivity_Visitor";

    private ClipViewLayout_Visitor mClipViewLayout1;
    private RelativeLayout mBottom;
    private TextView mBtn_cancel;
    private TextView mBt_ok;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_clip_image_visitor);

        Eyes_Visitor.setStatusBarColor(ClipImageActivity_Visitor.this, Color.parseColor("#70A0EC"));  //translucent 透明的

        mClipViewLayout1 = (ClipViewLayout_Visitor) findViewById(R.id.clipViewLayout1);
        mBottom = (RelativeLayout) findViewById(R.id.bottom);
        mBtn_cancel = (TextView) findViewById(R.id.btn_cancel);
        mBt_ok = (TextView) findViewById(R.id.bt_ok);

        mBtn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                generateUriAndReturn();
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        mClipViewLayout1.setImageSrc(getIntent().getData());
    }

    /**
     * 生成Uri并且通过setResult返回给打开的activity
     */
    private void generateUriAndReturn() {
        //调用返回剪切图
        Bitmap zoomedCropBitmap;

        zoomedCropBitmap = mClipViewLayout1.clip();

        if (zoomedCropBitmap == null) {
            Log.e("android", "zoomedCropBitmap == null");
            return;
        }
        Uri mSaveUri = Uri.fromFile(new File(getCacheDir(), "cropped_" + System.currentTimeMillis() + ".jpg"));
        if (mSaveUri != null) {
            OutputStream outputStream = null;
            try {
                outputStream = getContentResolver().openOutputStream(mSaveUri);
                if (outputStream != null) {
                    zoomedCropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                }
            } catch (IOException ex) {
                Log.e("android", "Cannot open file: " + mSaveUri, ex);
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            Intent intent = new Intent();
            intent.setData(mSaveUri);
            setResult(RESULT_OK, intent);
            finish();
        }
    }


}
